package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.BompAppParamConstant;
import cc.rengu.igas.bomp.common.dao.IndustryInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.IndustryInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.IndustryInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.IndustryInfoBean;
import cc.rengu.igas.bomp.facade.request.IndustryInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.IndustryInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 行业类别列表查询服务
 * <p>
 * Edit by 赵奎伍
 */
public class IndustryInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        IndustryInfoQueryRequest industryInfoQueryRequest = new IndustryInfoQueryRequest();
        ConvertUtil.convertOutput(industryInfoQueryRequest);
        return industryInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        IndustryInfoQueryRequest requestParams = (IndustryInfoQueryRequest) request;
        IndustryInfoQueryResponse queryResponse = new IndustryInfoQueryResponse();
        BeanUtil.beanCopy(requestParams, queryResponse);
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setResult(queryResponse);

        //region 查询字段
        //经营范围
        String managementScope = requestParams.getManagementScope();
        //行业编号
        String industryCode = requestParams.getIndustryCode();
        //行业名称
        String industryName = requestParams.getIndustryName();
        //行业描述
        String industryDesc = requestParams.getIndustryDesc();
        int pageIndex = requestParams.getPageIndex();
        int pageSize = requestParams.getPageSize();
        rglog.debug("查询条件:managementScope<{}>,industryCode<{}>,industryName<{}>,industryDesc<{}>,pageIndex<{}>,pageSize<{}>",
                managementScope, industryCode, industryName, industryDesc, pageIndex, pageSize);
        //endregion

        /* 根据行业编号、行业名称、行业描述、对公私专用标识查询行业类别信息 */
        IndustryInfoMapper industryInfoMapper = new IndustryInfoMapperImpl();
        List<IndustryInfo> industryInfoList = new ArrayList<>();
        if (-1 == pageIndex || -1 == pageSize) {
            industryInfoList = industryInfoMapper.selectIndustryInfoByKeys(managementScope, industryCode, industryName, industryDesc);
            if (CollectionUtils.isEmpty(industryInfoList)) {
                rglog.info("根据查询条件查询到0条行业类别");
                queryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                queryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                queryResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                queryResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            }
        } else {
            PageInfo pageInfo = new PageInfo();
            pageInfo.setPageIndex(pageIndex);
            pageInfo.setPageSize(pageSize);
            PageResult pageResult = industryInfoMapper.selectIndustryInfoPageByKeys(managementScope, industryCode, industryName, industryDesc, pageInfo);
            if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult() || pageResult.getResult().isEmpty()) {
                rglog.info("根据查询条件查询到0条行业类别");
                queryResponse.setTotalPage(BompAppParamConstant.INT_ZERO);
                queryResponse.setTotalRows(BompAppParamConstant.INT_ZERO);
                queryResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                queryResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
                bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            }

            /* 根据取出记录条数做对应处理 */
            for (Object object : pageResult.getResult()) {
                industryInfoList.add((IndustryInfo) object);
            }
            queryResponse.setTotalPage(pageResult.getPageInfo().getTotalPage());
            queryResponse.setTotalRows(pageResult.getPageInfo().getTotalRows());
        }


        if (!CollectionUtils.isEmpty(industryInfoList)) {
            /* 按照创建时间降序排序 */
            //查询语句已按照创建时间降序
            // industryInfoList = industryInfoList.stream().sorted(Comparator.comparing(IndustryInfo::getCreateTime)).collect(Collectors.toList());
            // Collections.reverse(industryInfoList);
            List<IndustryInfoBean> industryInfoBeans = new ArrayList<>();
            industryInfoList.forEach(item -> convIndustryInfo(item, industryInfoBeans));
            queryResponse.setIndustrylInfoList(industryInfoBeans);
        } else {
            rglog.error("根据经营范围、行业编号、行业名称、行业描述 无法查询到行业类别信息");
        }


        rglog.info("SIZE=<{}>", industryInfoList.size());
        /* 按照接口转换基础产品信息并响应给请求方 */
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        IndustryInfoQueryResponse industryInfoQueryResponse = (IndustryInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(industryInfoQueryResponse);
    }

    /**
     * 将数据库表结构对象转换成接口对象
     *
     * @param industryInfo      数据库表结构对象
     * @param industryInfoBeans 接口对象
     */
    private void convIndustryInfo(IndustryInfo industryInfo, List<IndustryInfoBean> industryInfoBeans) {
        if ("01".equals(industryInfo.getStatus())) {
            IndustryInfoBean industryInfoBean = new IndustryInfoBean();
            BeanUtil.beanCopy(industryInfo, industryInfoBean);
            industryInfoBean.setIndustryCode(industryInfo.getIndustryType());
            industryInfoBeans.add(industryInfoBean);
        }
    }
}
